<?php

/*
 * 平安银行支付demo
 * 
 * 
 * @author   ken(695093513@qq.com)
 * @copyright (c) 2015-08, pingan
 * 
 */

class pay {

    private $notify_url = 'http://dev.test.com/pingan_php/notify.php';
    private $return_url = 'http://dev.test.com/pingan_php/return.php';
    private $gateway = 'https://testebank.sdb.com.cn:461/corporbank/khPayment.do';

    /**
     * 支付开始
     */
    public function run() {
        //组装订单数据
        $data = array(
            'masterId' => '2000311146',
            'orderId' => '200031114620150604' . rand(10000000, 99999999),
            'currency' => 'RMB',
            'amount' => '0.01',
            'objectName' => 'pingan pay test',
            'paydate' => '20150604143506',
            'remark' => 'this is a test product',
            'validtime' => '0',
        );

        $data = $this->_getData($data);

        $merchantCertFile = 'merchant/2000311146.pfx';
        
        //获取orig和sign
        list($orig, $sign) = $this->_getOrigAndSing($merchantCertFile, $data);

        $parameter = array(
            'orig' => $orig,
            'sign' => $sign,
            'returnurl' => $this->return_url,
            'NOTIFYURL' => $this->notify_url,
        );

        $this->showHtml($parameter);
    }

    /**
     * 进行数据提交
     * @param array $parameter
     */
    private function showHtml($parameter) {
        $html = '<html><head><meta charset="UTF-8" /></head><body>';
        $html .= '<form method="post" name="P_FORM" action="' . $this->gateway . '">';
        foreach ($parameter as $key => $val) {
            $html .= "<input type='hidden' name='$key' value='$val' />";
        }
        $html .= '</form><script type="text/javascript">document.P_FORM.submit();</script>';
        $html .= '</body></html>';
        echo $html;
    }

    /**
     * 获取签名过后的原始数据orig和签名数据sign
     * @param file $merchantCertFile
     * @param string $data
     * @return list
     */
    private function _getOrigAndSing($merchantCertFile, $data) {
        require_once 'lib/pingan.php';
        $pingan = new pingan();

        $orig = $pingan->getOrig($data);
        $sign = $pingan->getSign($merchantCertFile, $data);

        return array($orig, $sign);
    }

    /**
     * 将数据组装为xml格式的数据
     * @param array $data
     * @return string
     * 
     * $data = '<kColl id="input" append="false"><field id="masterId" value="2000311146"/><field id="orderId" value="20003111462015060473550416"/><field id="currency" value="RMB"/><field id="amount" value="0.01"/><field id="objectName" value="��"/><field id="paydate" value="20150604143506"/><field id="remark" value="��"/><field id="validtime" value="0"/></kColl>';
     */
    private function _getData($data) {
        $xml_data = '<kColl id="input" append="false">';
        foreach ($data as $key => $value) {
            $xml_data .= '<field id="' . $key . '" value="' . $value . '"/>';
        }
        $xml_data .= '</kColl>';

        return $xml_data;
    }

}

//运行程序
$payClass = new pay();
$payClass->run();
